<?php

/*
+--------------------------------------------------------------------------
|   Invision Power Board
|   =============================================
|   by Matthew Mecham
|   (c) 2001 - 2006 Invision Power Services, Inc.
|   
|   =============================================
|   
|   
+---------------------------------------------------------------------------
|   > $Date: 2006-09-22 06:28:31 -0400 (Fri, 22 Sep 2006) $
|   > $Revision: 567 $
|   > $Author: matt $
+---------------------------------------------------------------------------
|
|   > Statistical functions
|   > Module written by Matt Mecham
|   > Date started: 4th July 2002
|
|	> Module Version Number: 1.0.0
|   > DBA Checked: Tue 25th May 2004
+--------------------------------------------------------------------------
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class ad_bank
{
	var $base_url;
	var $month_names = array();
	
	/**
	* Section title name
	*
	* @var	string
	*/
	var $perm_main = "admin";
	
	/**
	* Section title name
	*
	* @var	string
	*/
	var $perm_child = "bank";
	
	
	function auto_run()
	{
		//-----------------------------------------
		
		$this->month_names = array( 1 => 'January', 'February', 'March'     , 'April'  , 'May'     , 'June',
										 'July'   , 'August'  , 'September' , 'October', 'November', 'December'
								  );

		switch($this->ipsclass->input['code'])
		{
			case 'config_showedit':
				$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':edit' );
				$this->config_form('edit');
				break;
			
			case 'config_edit':
				$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':add' );
				$this->config_save('edit');
				break;
				
			case 'config_add':
				$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':add' );
				$this->config_save('add');
				break;
				
			case 'config':
				$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':show' );
				$this->show_config_page();
				break;
				
			default:
				$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':show' );
				$this->main_screen();
				break;
		}
		
	}
	
//-----------------------------------------
	//
	// New setting form
	//
	//-----------------------------------------

	function config_form( $type='add' )
	{
		$this->ipsclass->admin->page_title  = "Bank Management";
		$this->ipsclass->admin->page_detail = "This section contains the functions to manage your IPB bank.";
		$this->ipsclass->admin->nav[]       = array( '', "Add/Edit Configuration" );

		if ( $type == 'add' )
		{
			$formcode = 'config_add';
			$title    = "Create New Configuration";
			$button   = "Add Configuration";
			$config = array( 	'bank_config_name' 	=> '',
								'bank_config_desc' 	=> '',
								'bank_config_value'	=> '',
								'bank_config_key'	=> '',
							   );

		}
		else
		{
			$config = $this->ipsclass->DB->simple_exec_query( array( 'select' => '*', 'from' => 'bank_configs', 'where' => 'bank_config_id='.intval($this->ipsclass->input['id']) ) );

			if ( ! $config['bank_config_id']) {
				$this->ipsclass->main_msg = "No ID was passed, please try again.";
				$this->show_config_page();
			}

			$formcode = 'config_edit';
			$title    = "Edit Configuration <u>".$config['bank_config_name']."</u>";
			$button   = "Edit Configuration";
		}

		$this->ipsclass->admin->page_detail = '&nbsp;';
		$this->ipsclass->admin->page_title  = $title;

		//-----------------------------------------
		// start form
		//-----------------------------------------

		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'code'   , $formcode ),
																			 2 => array( 'act'    , 'bank'      ),
																			 3 => array( 'id'     , $this->ipsclass->input['id'] ),
																			 4 => array( 'section', 'content' ),
																	)      );

		//-----------------------------------------
		// start table
		//-----------------------------------------

		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "60%" );

		//-----------------------------------------
		// um..
		//-----------------------------------------

		$this->ipsclass->html .= $this->ipsclass->adskin->start_table( $title );
												 		    	
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Configuration Name</b>" ,
												  			   $this->ipsclass->adskin->form_input( 'config_name', ( isset($_POST['config_name']) AND $_POST['config_name'] ) ? $_POST['config_name'] : $config['bank_config_name'] )
										 		    	)      );

		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Configuration Description</b>" ,
												  			   $this->ipsclass->adskin->form_textarea( 'config_desc', ( isset($_POST['config_desc']) AND $_POST['config_desc'] ) ? $_POST['config_desc'] : $config['bank_config_desc'] )
										 		    	)      );

		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Configuration Key</b>" ,
												  			   $this->ipsclass->adskin->form_input( 'config_key', ( isset($_POST['config_key']) AND $_POST['config_key'] ) ? $_POST['config_key'] : $config['bank_config_key'] )
										 		    	)      );

		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Configuration Value</b>" ,
												  			   $this->ipsclass->adskin->form_input( 'config_value', ( isset($_POST['config_value']) AND $_POST['config_value'] ) ? $_POST['config_value'] : $config['bank_config_value'] )
										 		    	)      );
										 		    									 		    	
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form( $button );

		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();

		$this->ipsclass->admin->output();

	}
	
	//-----------------------------------------
	// Configuration Save Form
	//-----------------------------------------

	function config_save($type='add')
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------

		$config_name 	= $this->ipsclass->input['config_name'];
		$config_key 	= $this->ipsclass->input['config_key'];
		//-----------------------------------------
		// Check...
		//-----------------------------------------

		if ( ! $config_name )
		{
			$this->ipsclass->main_msg = "You must enter configuration name.";
			$this->config_form();
			return;
		}
		
		if ( ! $config_key )
		{
			$this->ipsclass->main_msg = "You must enter configuration key.";
			$this->config_form();
			return;
		}
		
		$config_desc = $this->ipsclass->input['confg_desc'];
		$config_value = $this->ipsclass->input['config_value'];
		
		//-----------------------------------------
		// check...
		//-----------------------------------------

		$array = array( 'bank_config_name' 	=> $config_name,
						'bank_config_desc' 	=> $config_desc,
						'bank_config_key' 	=> $config_key,
						'bank_config_value'	=> $config_value
					 );


		if ( $type == 'add' )
		{
			$this->ipsclass->DB->do_insert( 'bank_configs', $array );
			$this->ipsclass->main_msg = 'New Configuration Added';
		}
		else
		{
			$this->ipsclass->DB->do_update( 'bank_configs', $array, 'bank_config_id='.intval($this->ipsclass->input['id']) );
			$this->ipsclass->main_msg = 'Configuration Edited';
		}

		$this->show_config_page();
	}
	
	function show_config_page() {
		$this->ipsclass->admin->page_title  = "Bank system variable management";
		$this->ipsclass->admin->page_detail = "This section contains the functions to manage your IPB bank.";

		//-----------------------------------------
		// start table
		//-----------------------------------------

		$this->ipsclass->adskin->td_header[] = array( " "  , "2%" );
		$this->ipsclass->adskin->td_header[] = array( "Configurations"  , "38%" );
		$this->ipsclass->adskin->td_header[] = array( "Key"  , "25%" );
		$this->ipsclass->adskin->td_header[] = array( "Value"  , "25%" );
		$this->ipsclass->adskin->td_header[] = array( "Actions"  , "10%" );

		$basic_title = "<table cellpadding='0' cellspacing='0' border='0' width='100%'>
						<tr>
						 <td align='left' width='40%' style='font-size:12px; vertical-align:middle;font-weight:bold; color:#FFF;'>Bank configuration management</td>
						</tr>
						</table>";

		$this->ipsclass->html .= $this->ipsclass->adskin->start_table($basic_title);

		//-----------------------------------------
		// Get categories
		//-----------------------------------------

		$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'bank_configs'));
		$this->ipsclass->DB->simple_exec();

		while( $config = $this->ipsclass->DB->fetch_row() )
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<div align='center'><img src='{$this->ipsclass->adskin->img_url}/images/settings_folder.gif' border='0' alt='Folder' /></div>",
																   "<div><b>{$config['bank_config_name']}</b><div style='color:gray'>{$config['bank_config_desc']}</div>" ,
																	"<b>{$config['bank_config_key']}</b>" ,"{$config['bank_config_value']}",
												  				   array("<div align='center' style='white-space:nowrap'>
												  				          <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code}&code=config_showedit&id={$config['bank_config_id']}' title='Edit this configuration'><img src='{$this->ipsclass->adskin->img_url}/images/icons_menu/edit.gif' border='0' alt='Edit'  /></a>
																          <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code}&code=config_delete&id={$config['bank_config_id']}' title='Delete this configuration'><img src='{$this->ipsclass->adskin->img_url}/images/icons_menu/delete.gif' border='0' alt='Delete'  /></a>
																          </div>", 1, 'tablerow3' )
										 					  )      );
		}

		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();


		//-----------------------------------------
		// Import partial settings?
		//-----------------------------------------

		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'code'          , 'config_add'    ),
																 2 => array( 'act'           , 'bank'        ),
																 3 => array( 'section', $this->ipsclass->section_code ),
													 ) , "configaddform");

		//-----------------------------------------

		$this->ipsclass->adskin->td_header[] = array( "&nbsp;" , "50%" );
		$this->ipsclass->adskin->td_header[] = array( "&nbsp;" , "50%" );

		//-----------------------------------------

		$this->ipsclass->html .= $this->ipsclass->adskin->start_table( "Add New Configuration" );

		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Configuration name</b>" ,
										  				        			 $this->ipsclass->adskin->form_input( 'config_name' )
											                        )      );

		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Conguration description</b>" ,
										  				         			$this->ipsclass->adskin->form_textarea( 'config_desc')
											                        )      );

		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Configuration key</b>" ,
										  				         			  $this->ipsclass->adskin->form_input( 'config_key')
																	)      );
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Configuration value</b>" ,
										  				         			  $this->ipsclass->adskin->form_input( 'config_value')
																	)      );																	

		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Add Configuration");

		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();

		$this->ipsclass->admin->output();	
	}
	
	//-----------------------------------------
	//| Date selection screen
	//-----------------------------------------
	
	function main_screen()
	{
		$this->ipsclass->admin->page_title = "Bank Statistic Center";
		$this->ipsclass->admin->nav[] = array( $this->ipsclass->form_code, 'Bank Statistics Overview' );
		$this->ipsclass->admin->page_detail = "Please define the date ranges and other options below.<br>Note: The statistics generated are based on the information currently held in the database, they do not take into account pruned forums or delete posts, etc.";
		
		$form_code = 'show_history';
		$table     = 'Even Bank Statistics';
		
		$old_date = getdate(time() - (3600 * 24 * 90));
		$new_date = getdate(time() + (3600 * 24));
		
		
		//-----------------------------------------
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'code'  , $form_code  ),
												  2 => array( 'act'   , 'bank'     ),
												  4 => array( 'section', $this->ipsclass->section_code ),
									     )      );
		
		//-----------------------------------------
		
		$this->ipsclass->adskin->td_header[] = array( "&nbsp;"  , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "&nbsp;"  , "60%" );
		
		//-----------------------------------------
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table( $table );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Member name</b>" ,
												  $this->ipsclass->adskin->form_input( "log_member" , $this->ipsclass->input['log_member'] )
									     )      );
									     
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Log Type</b>" ,
												  $this->ipsclass->adskin->form_dropdown( "log_type" , $this->get_log_type(), intval($this->ipsclass->input['log_type']) )
									     )      );
									     
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Date From</b>" ,
												  $this->ipsclass->adskin->form_dropdown( "from_month" , $this->make_month(), $old_date['mon']  ).'&nbsp;&nbsp;'.
												  $this->ipsclass->adskin->form_dropdown( "from_day"   , $this->make_day()  , $old_date['mday'] ).'&nbsp;&nbsp;'.
												  $this->ipsclass->adskin->form_dropdown( "from_year"  , $this->make_year() , $old_date['year'] )
									     )      );
									     
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Date To</b>" ,
												  $this->ipsclass->adskin->form_dropdown( "to_month" , $this->make_month(), $new_date['mon']  ).'&nbsp;&nbsp;'.
												  $this->ipsclass->adskin->form_dropdown( "to_day"   , $this->make_day()  , $new_date['mday'] ).'&nbsp;&nbsp;'.
												  $this->ipsclass->adskin->form_dropdown( "to_year"  , $this->make_year() , $new_date['year'] )
									     )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Show");
										 
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		
		// Show the log result
		$table_result = "Even Bank Stats Result";
		
		$this->ipsclass->adskin->td_header[] = array( "ID"  , "10%" );
		$this->ipsclass->adskin->td_header[] = array( "Message"  , "30%" );
		$this->ipsclass->adskin->td_header[] = array( "Time"  , "20%" );
		$this->ipsclass->adskin->td_header[] = array( "Amount"  , "20%" );
		$this->ipsclass->adskin->td_header[] = array( "Balance"  , "20%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table( $table_result );
		
		// Init end time
		$end_time = time();
		$start_time = $end_time - 7*86400;
		
		if($this->ipsclass->input['log_type'] > 0) {
			$condition .= " AND log_type=".intval($this->ipsclass->input['log_type']);
		}
		
		if($this->ipsclass->input['to_month']
		&& $this->ipsclass->input['to_day']
		&& $this->ipsclass->input['to_year']
		&& $this->ipsclass->input['from_month']
		&& $this->ipsclass->input['from_day']
		&& $this->ipsclass->input['from_year']) {
			$end_time   = mktime(0 ,0 ,0 ,$this->ipsclass->input['to_month']   ,$this->ipsclass->input['to_day']   ,$this->ipsclass->input['to_year']  );
			$start_time = mktime(0 ,0 ,0 ,$this->ipsclass->input['from_month'] ,$this->ipsclass->input['from_day'] ,$this->ipsclass->input['from_year']);
		}
		
		if(trim($this->ipsclass->input['log_member'])) {
			$member_name = trim($this->ipsclass->input['log_member']);
			
			$this->ipsclass->DB->simple_construct(array('select'	=> 'id',
														'from'		=> 'members',
														'where'		=> "name='".$member_name."' OR members_display_name='".$member_name."'"
														)
													);
			$this->ipsclass->DB->simple_exec();
			
			$member_ids = array();
			while($member = $this->ipsclass->DB->fetch_row()) {
				$member_ids[] = $member['id'];
			}
			
			if(count($member_ids) > 0)	$condition .= " AND member_id IN(".implode(",", $member_ids).")";
		}
		
		$this->ipsclass->DB->simple_construct(array('select'	=> '*',
													'from'		=> 'bank_logs',
													'where'		=> "log_time BETWEEN ".$start_time." AND ".$end_time." ".$condition
													)
												);
		$this->ipsclass->DB->simple_exec();
		
		$running_total = 0;
		while($log = $this->ipsclass->DB->fetch_row()) {
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array($log['log_id'],
												  								$log['log_message'],
												  								"<div align='right'>".$this->ipsclass->get_date($log['log_time'])."</div>",
												  								"<div align='right'>\$".number_format($log['log_amount'])."</div>",
												  								"<div align='right'>\$".number_format($log['log_balance'])."</div>"
									     )      );
									     
			$running_total += $log['log_amount'];
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( '&nbsp;','&nbsp;',
													 "<div align='right'><b>Total</b></div>",
													 "<div align='right'><b>\$".number_format($running_total)."</b></div>",'&nbsp;'
											)      );
											
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
							
		$this->ipsclass->admin->output();
		
		
	}
	
	//-----------------------------------------
	
	function get_log_type() {
		
		$log_type = array(	array(0, "All"),
							array(1, "Deposit to Even Bank"),
							array(2, "Withdraw from Even Bank"),
							array(3, "Transfer to other members"),
							array(4, "Receiving from other members"),
							array(5, "Deposit by GATE Card"),
							);
		
		return $log_type;
	}
	
	function make_year()
	{
		$time_now = getdate();
		
		$return = array();
		
		$start_year = 2002;
		
		$latest_year = intval($time_now['year']);
		
		if ($latest_year == $start_year)
		{
			$start_year -= 1;
		}
		
		for ( $y = $start_year; $y <= $latest_year; $y++ )
		{
			$return[] = array( $y, $y);
		}
		
		return $return;
	}
	
	//-----------------------------------------
	
	function make_month()
	{
		$return = array();
		
		for ( $m = 1 ; $m <= 12; $m++ )
		{
			$return[] = array( $m, $this->month_names[$m] );
		}
		
		return $return;
	}
	
	//-----------------------------------------
	
	function make_day()
	{
		$return = array();
		
		for ( $d = 1 ; $d <= 31; $d++ )
		{
			$return[] = array( $d, $d );
		}
		
		return $return;
	}
	
	
		
}


?>